package com.bawei.demoa.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bawei.demoa.R;
import com.bawei.demoa.fragment.ZiXunFragment;

import java.util.ArrayList;

public class MainActivity extends FragmentActivity {

    private ArrayList<Fragment> list;
    private RadioButton header1;
    private RadioButton header2;
    private RadioButton header3;
    private RadioButton header4;
    private ArrayList<RadioButton> radioList;
    private int indicatorWidth;
    private TextView text1;
    private TextView text2;
    private TextView text3;
    private TextView text4;
    private ArrayList<TextView> textList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //找控件
        final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
        RadioGroup group = (RadioGroup) findViewById(R.id.group);
        header1 = (RadioButton) findViewById(R.id.header1);
        header2 = (RadioButton) findViewById(R.id.header2);
        header3 = (RadioButton) findViewById(R.id.header3);
        header4 = (RadioButton) findViewById(R.id.header4);
        text1 = (TextView) findViewById(R.id.text1);
        text2 = (TextView) findViewById(R.id.text2);
        text3 = (TextView) findViewById(R.id.text3);
        text4 = (TextView) findViewById(R.id.text4);

        radioList = new ArrayList<>();
        radioList.add(header1);
        radioList.add(header2);
        radioList.add(header3);
        radioList.add(header4);

        textList = new ArrayList<>();
        textList.add(text1);
        textList.add(text2);
        textList.add(text3);
        textList.add(text4);

        //准备数据
        data();

        viewPager.setOffscreenPageLimit(4);

        //给viewpager设置适配器
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });

        //给viewpager设置切换监听
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i <= radioList.size(); i++) {
                    if (i == position) {
                        radioList.get(i).setChecked(true);
                        textList.get(i).setVisibility(View.VISIBLE);
                    } else {
                        radioList.get(i).setChecked(false);
                        textList.get(i).setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //给RadioGroup设置监听
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.header1:
                        header1.setTextColor(Color.GREEN);

                        text1.setVisibility(View.VISIBLE);

                        text2.setVisibility(View.GONE);
                        text3.setVisibility(View.GONE);
                        text4.setVisibility(View.GONE);
                        header2.setTextColor(Color.BLACK);
                        header3.setTextColor(Color.BLACK);
                        header4.setTextColor(Color.BLACK);
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.header2:
                        header2.setTextColor(Color.GREEN);
                        text2.setVisibility(View.VISIBLE);

                        text1.setVisibility(View.GONE);
                        text3.setVisibility(View.GONE);
                        text4.setVisibility(View.GONE);

                        header1.setTextColor(Color.BLACK);
                        header3.setTextColor(Color.BLACK);
                        header4.setTextColor(Color.BLACK);
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.header3:
                        header3.setTextColor(Color.GREEN);
                        text3.setVisibility(View.VISIBLE);

                        text2.setVisibility(View.GONE);
                        text1.setVisibility(View.GONE);
                        text4.setVisibility(View.GONE);

                        header2.setTextColor(Color.BLACK);
                        header1.setTextColor(Color.BLACK);
                        header4.setTextColor(Color.BLACK);
                        viewPager.setCurrentItem(2);
                        break;
                    case R.id.header4:
                        header4.setTextColor(Color.GREEN);
                        text4.setVisibility(View.VISIBLE);

                        text2.setVisibility(View.GONE);
                        text3.setVisibility(View.GONE);
                        text1.setVisibility(View.GONE);

                        header2.setTextColor(Color.BLACK);
                        header3.setTextColor(Color.BLACK);
                        header1.setTextColor(Color.BLACK);
                        viewPager.setCurrentItem(3);
                        break;
                }
            }
        });
    }

    /**
     * 定义准备数据的方法
     */
    private void data() {
        list = new ArrayList<>();
        list.add(new ZiXunFragment());
        list.add(new ZiXunFragment());
        list.add(new ZiXunFragment());
        list.add(new ZiXunFragment());
    }
}
